<?php
namespace App\Http\Controllers\Seek;

use App\Http\Controllers\Controller;
use App\Http\Requests\Seek\PostRequest;
use App\Repositories\Seek\FindNoticeRepository;
use App\Repositories\Seek\FindImagesRepository;
use App\Repositories\Seek\CludRepository;
use Illuminate\Support\Facades\DB;

class SeekController extends Controller {

    public function post(PostRequest $request, FindNoticeRepository $repository, FindImagesRepository $findImagesRepository, CludRepository $cludRepository)
    {
        $data = $request->all();
        $uid = $data['uid'];
        unset($data['uid']);
        DB::beginTransaction();
        $nid = $repository->posts($data, $uid);
        if ( $nid ) {
            if ( $findImagesRepository->addImages($data, $nid) && $cludRepository->addClud($data, $nid) ) {
                DB::commit();
                return success([]);
            }
        }
        DB::rollBack();
        return failed("未知错误,添加失败");
    }

    public function isPost($uid, FindNoticeRepository $repository)
    {
        $res = $repository->isPost($uid);
        if ($res) return success([]);
        return failed("没有查询到数据哦!", 204);
    }

    public function newlists(FindNoticeRepository $repository)
    {
        $res = $repository->getNewLists();
        if($res) return success($res);
        return failed("查询失败");
    }

    public function searchnotice(FindNoticeRepository $repository)
    {
        $res = $repository->searchNoticeLists();
        if ($res) return success($res);
        failed("查询失败");
    }

    public function detail($id, FindNoticeRepository $repository)
    {
        $res = $repository->detail($id);
        if($res) {
            $time = strtotime($res['clud']['date'].' '.(strlen($res['clud']['time'])==1?"0".$res['clud']['time']:$res['clud']['time']).":00:00");
            $h = ceil((time()-$time) / 3600);
            $radius = $h*100000;
            $res['disabled'] = false;
            if ($radius > 5500000) {
                $radius = 5500000;
                $res['disabled'] = true;
            }
            $res['level'] = 16;
            foreach (config("config.mapLevel") as $key => $val) {
                if ($radius >= $val) {
                    $res['level'] = $key;
                    break;
                }
            }
            $res['radius'] = $radius;
            return success($res);
        }
        return failed("没找到数据");
    }

}